package com.web.netalbum.controller;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.web.netalbum.bean.Album;
import com.web.netalbum.bean.User;
import com.web.netalbum.service.AlbumSevice;
import com.web.netalbum.service.PhotoService;
import com.web.netalbum.service.UserService;
import com.web.netalbum.service.impl.AlbumSeviceImpl;
import com.web.netalbum.service.impl.PhotoServiceImpl;
import com.web.netalbum.service.impl.UserServiceImpl;

/**
 * 文件上传的处理类
 * @author lyf
 *
 */
public class FileUpLoad extends HttpServlet {
	private static final long serialVersionUID = 4617141886678818728L;
	
	private static AlbumSevice as=new AlbumSeviceImpl();
	private static PhotoService ps=new PhotoServiceImpl();
	private static UserService us=new UserServiceImpl();
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		FileItemFactory factory=new DiskFileItemFactory();
		ServletFileUpload upload=new ServletFileUpload(factory);
		List<FileItem> fileItems;
		StringBuilder sb=new StringBuilder();
		String album=null;
		String userName=null;
		List<String> listNames=new ArrayList<>();
		//List<String> names=new ArrayList<>();
		User user=null;
		Album al=null;
		try {
			fileItems=upload.parseRequest(req);
			if (fileItems!=null) {
				String path = getServletContext().getRealPath("/images");
				int i = 0;
				for (FileItem fileItem : fileItems) {
					if(fileItem.isFormField()){
						String fieldName = fileItem.getFieldName();
						if(fieldName.equals("album")){
							album = fileItem.getString();
							System.out.println(fieldName+"="+album);
							al=as.findAlbumByName(album);
							
						}else if(fieldName.equals("userName")){
							userName=fileItem.getString();
							System.out.println(fieldName+"="+userName);
							 user= us.findUserByName(userName);
						}
					}else{
						String fileName=System.currentTimeMillis()+fileItem.getName();
						//names.add(fileItem.getName());
						listNames.add(fileName);
						fileItem.write(new File(path, fileName));
						System.out.println(fileItem.getName());
					}
				}
				Object[][] args=new Object[listNames.size()][];
				SimpleDateFormat sFormat=new SimpleDateFormat("yyyy-MM-dd");
				String date = sFormat.format(new Date());
				sb.append("[");
				for(int j=0;j<listNames.size();j++){
					sb.append("{");
					String path1=listNames.get(j);
					//String name=names.get(j);
					args[j]=new Object[5];
					args[j][0]=path1;
					sb.append("\"photoName\":\""+path1+"\"");
					sb.append(",");
					args[j][1]=path1;
					sb.append("\"path\":\""+"images/"+path1+"\"");
					args[j][2]=date;
					args[j][3]=user.getId();
					args[j][4]=al.getId();
					sb.append("}");
					if(j!=listNames.size()-1){
						sb.append(",");
					}
				}
				sb.append("]");
				ps.savePhotos(args);
			}
			System.out.println(sb.toString());
			resp.getWriter().write(sb.toString());
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doGet(req, resp);
	}

}
